﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LoginDemo.ViewModel.Common;

namespace LoginDemo.ViewModel.Login
{
    public class LoginViewModel : NotificationObject
    {
        public LoginViewModel() 
        {
            model.UserName = "test";
        }

        private LoginModel model = new LoginModel();

        /// <summary>
        /// 用户名
        /// </summary>
        [NotEmptyCheck]
        [UserNameExists]
        public string UserName 
        {
            get 
            {
                return model.UserName;
            }
            set 
            {
                model.UserName = value;
                this.RaisePropertyChanged(nameof(model.UserName));
            }
        }

        /// <summary>
        /// 密码
        /// </summary>
        [NotEmptyCheck]
        public string Password
        {
            get
            {
                return model.Password;
            }
            set
            {
                model.Password = value;
                this.RaisePropertyChanged("Password");
            }
        }

        /// <summary>
        /// 性别
        /// </summary>
        public int Gender
        {
            get
            {
                return model.Gender;
            }
            set
            {
                model.Gender = value;
                this.RaisePropertyChanged("Gender");
            }
        }


        private bool toClose = false;
        /// <summary>
        /// 是否要关闭窗口
        /// </summary>
        public bool ToClose
        {
            get
            {
                return toClose;
            }
            set
            {
                toClose = value;
                if (toClose)
                {
                    this.RaisePropertyChanged("ToClose");
                }
            }
        }


        private BaseCommand clickLoginCommand;
        public BaseCommand ClickLoginCommand 
        {
            get 
            {
                if (clickLoginCommand == null) 
                {
                    clickLoginCommand = new BaseCommand(LoginExce);
                }
                return clickLoginCommand;
            }
        }

        public void LoginExce(object obj)
        {
            // 执行登录逻辑
            //UserName = "张三";
            WindowManager.Show("MainWindow", null); 
            ToClose = true;
        }

        /// <summary>
        /// 数据填写正确
        /// </summary>
        public override bool IsValid
        {
            get
            {
                return model.IsValid;
            }
            set
            {
                if (value == model.IsValid)
                {
                    return;
                }
                model.IsValid = value;
                this.RaisePropertyChanged("IsValid");
            }
        }
    }
}
